package com.algo.Jaya;

import java.util.Arrays;

/**
 * @Author: guoxing
 * @Email: gx_study2022@163.com
 * @Description: https://blog.csdn.net/qq_37853160/article/details/127273019
 * @Date: 2024/6/24 22:27
 * @ClassName: Population
 **/
public class Population {

    private Individual[] population;

    private Individual best;
    private Individual worst;

    public Individual getBest() {
        return this.best;
    }

    private void setBest() {
        this.best = this.getFittest(0);
    }

    public void setBest1(Individual best){
        this.best = best;
    }

    public Individual getWorst() {
        return this.worst;
    }

    private void setWorst() {
        this.worst = this.getFittest(this.size()-1);
    }

    public void setWorst1(Individual worst){
        this.worst = worst;
    }

    public Individual[] getIndividuals(){
        return this.population;
    }

    public void setIndividual(int offset, Individual individual){
        this.population[offset] = individual;
    }

    public Individual getIndividual(int offset){
        return this.population[offset];
    }

    public Population(int popNum){
        population = new Individual[popNum];
    }

    public Population(RawData rawData, int popNum){
        this(popNum);
        for (int i = 0; i < popNum; i++) {
            population[i] = new Individual(rawData);
        }
    }

    public int size(){
        return population.length;
    }

    public Individual getFittest(int offset){
        Arrays.sort(this.population, (o1, o2) -> o1.getFinishTime()-o2.getFinishTime());
        return this.population[offset];
    }
}

